package cn.flghtcloud.lucy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@SpringBootApplication
@EnableEurekaClient
@EnableHystrix // 开启断路器
@EnableHystrixDashboard // 开启HystrixDashboard
@RestController
public class ServiceLucyApplication {

	private static final Logger LOG = LoggerFactory.getLogger(ServiceLucyApplication.class.getName());

	public static void main(String[] args) {
		SpringApplication.run(ServiceLucyApplication.class, args);
	}

	@Autowired
	private RestTemplate restTemplate;

	@Bean
	public RestTemplate getRestTemplate() {
		return new RestTemplate();
	}

	@HystrixCommand(fallbackMethod = "hiError")
	@RequestMapping("/hi")
	public String callHome() {
		LOG.info("calling trace service-hi  ");
		return restTemplate.getForObject("http://localhost:8989/miya", String.class);
	}
	
	public String hiError() {
		return "hi,sorry,error!";
	}

	@RequestMapping("/info")
	public String info() {
		LOG.info("calling trace service-hi ");
		return "i'm service-hi";
	}

}
